<?php 
/**
 * Class model product 
 * @author NguyenVanTien
 */
class Application_Model_Products extends Application_Model_DbTable_Products
{

    /**
     * Constructure of class
     * Call parent of class
     * @author Tien Nguyen
     */
   public function __construct(){
        parent::__construct();
   } 
    
   /**
    * Get list product
    * with visible = 1 and is_deleted = 0 
    * @author Tien Nguyen
    * @return Zend_Db_Select   
    */
   public function getList(){
        $select = $this->select();
        $select->from('products');
        $select->where('products.visible = ?', 1);
        $select->where('products.is_delete = ?', 0);
        return $select;
   }
   
   /**
    * get list product with category id
    * @param int $categoryId
    * @return Zend_Db_Select 
    */
   public function getListProductCategory($categoryId){
        $select = $this->getList();
        $select->setIntegrityCheck(FALSE);
        $select->joinLeft('product_images', 
                'product_images.product_image_id = products.product_image_default',
                array('path as image_path', 'name as image_name'));
        $select->join('product_category_layout', 'product_category_layout.product_id
            = products.product_id', array());
        $select->where('product_category_layout.catalog_id = ?', $categoryId);
        $select->order(array('create_date DESC'));
        return $select;
   }
   
   /**
    * Get list products for index page
    * 
    * @author NguyenVanTien
    * @return Zend_Db_Select
    */
   public function getListLastProducts(){
        $select = $this->getList();
        $select->setIntegrityCheck(FALSE);
        $select->joinLeft('product_images', 
                'product_images.product_image_id = products.product_image_default',
                array('path as image_path', 'name as image_name'));
        $select->order(array('create_date DESC'));
        return $select;
   }
   
   /**
    * Get list product hot in product hot
    * @author Tien Nguyen
    * @return Zend_Db_Select 
    */
   public function getListHotProducts(){
       $select = $this->select();
       $select->from('product_hots', array());
       $select->setIntegrityCheck(FALSE)
              ->join(array('products' => $this->getList()), 
                      'products.product_id = product_hots.product_id')
              ->joinLeft('product_images', 
                      'product_images.product_image_id = products.product_image_default',
                      array('path as image_path', 'name as image_name'))
              ->order(array('product_hots.order ASC'));
       return $select;
   }
   
   /**
    * Get product with product id
    * @author Tien Nguyen
    * @return Zend_Db_Select
    */
   public function getProduct($productId){
       $select = $this->select();
       $select->from('products')
              ->where('product_id = ?', $productId);
       $row = $this->fetchRow($select);
       if(!is_null($row)){
           return $row->toArray();
       }
       return null;
   }
}